package com.zjmzxfzhl.common.util;

import com.zjmzxfzhl.common.exception.SysException;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.util.StringUtils;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class PathUtil {

    public static final String DEFAULT_PATH = "default";
    public static final String TEMP_PATH = "temp";
    public static final String USER_PROFILE = "profile";
    public static final String USER_PATH = DEFAULT_PATH + "/" + USER_PROFILE;

    /**
     * 获取默认路径
     *
     * @return
     */
    public static String getDefaultRootPath(String path) {
        String rootJarPath = getBaseJarPath().getAbsolutePath();
        File defaultFile = new File(rootJarPath + File.separator + path);
        if (!defaultFile.exists()) {
            if (!defaultFile.mkdirs()) {
                throw new SysException("创建默认存放路径失败");
            }
        }
        return defaultFile.getAbsolutePath();
    }


    public static File getBaseJarPath() {
        ApplicationHome home = new ApplicationHome(PathUtil.class);
        File jarFile = home.getSource();
        return jarFile.getParentFile();
    }

    /**
     * 获取用户头像路径
     *
     * @param userId
     * @return
     */
    public static String getUserProfileAbsolutePath(String userId) {
        String defaultRootPath = getDefaultRootPath(DEFAULT_PATH);
        String userPath = defaultRootPath + File.separator + getUserProfile(userId);
        return userPath;
    }

    public static String getUserProfile(String userId) {
        return USER_PROFILE + File.separator + userId + ".png";
    }


    /**
     * 获取文件存放绝对路径
     *
     * @param fileOriginalName
     * @return
     */
    public static String getFileAbsolutePath(String fileOriginalName) {
        String filePath = getDefaultRootPath(DEFAULT_PATH) + File.separator + getFileTimeName(fileOriginalName);
        return filePath;

    }

    /**
     * 获取文件存放绝对路径
     *
     * @param fileOriginalName
     * @return
     */
    public static String getTempFileAbsolutePath(String fileOriginalName) {
        String filePath = getDefaultRootPath(TEMP_PATH) + File.separator + getFileTimeName(fileOriginalName);
        return filePath;

    }


    /**
     * 获取默认路径
     *
     * @return
     */
    public static File getTempFile(String fileOriginalName) {
        return new File(getTempFileAbsolutePath(fileOriginalName));
    }

    /**
     * 获取默认路径
     *
     * @return
     */
    public static File getDefaultFile(String fileOriginalName) {
        return new File(getFileAbsolutePath(fileOriginalName));
    }

    /**
     * 根据文件原始名称,增加当前时间
     *
     * @param fileOriginalName
     * @return
     */
    public static String getFileTimeName(String fileOriginalName) {
        String extension = StringUtils.getFilenameExtension(fileOriginalName);
        String fileName = StringUtils.getFilename(fileOriginalName);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        if (StringUtils.isEmpty(extension)) {
            return fileName + time;
        } else {
            fileName = fileName.replace("." + extension, "");
            return fileName + time + "." + extension;
        }
    }


}
